package com.xxxy.dongyexuan.action;

import com.xxxy.dongyexuan.mapper.IProjectMapper;
import com.xxxy.dongyexuan.mapper.IUserMapper;
import com.xxxy.dongyexuan.po.PageBean;
import com.xxxy.dongyexuan.po.Project;
import com.xxxy.dongyexuan.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class ProjectAction {

    @Autowired
    private IProjectMapper mapper;


    //分页查询
    @GetMapping("/project/{pagenow}/{pagesize}")
    public PageBean findByPage(@PathVariable("pagenow") int pagenow, @PathVariable("pagesize") int pagesize){
        int from =(pagenow-1)*pagesize;//开始条数
        int total = this.mapper.getcount();//总条数
        int pages =(total-1)/pagesize+1;//计算总页数
        List data = this.mapper.findByPage(from,pagesize);
        PageBean pageBean = new PageBean();
        pageBean.setPages(pages);
        pageBean.setPagenow(pagenow);
        pageBean.setPagesize(pagesize);
        pageBean.setTotal(total);
        pageBean.setData(data);
        return pageBean;
    }



    //查询所有
    @GetMapping("/project")
    public List<Project> findAll(){
        List<Project> list = this.mapper.findAll();
        return list;
    }

    //根据员工号查询
    @GetMapping("/project/{pid}")
    public Project findById(@PathVariable("pid") int pid){
        Project project = this.mapper.findById(pid);
        return project;
    }

    //增加
    @PostMapping("/project")
    public int save(Project project){
        int result = this.mapper.save(project);
        return result;
    }

    //修改
    @PutMapping("/project")
    public int update(Project project){
        int result = this.mapper.merge(project);
        return result;
    }

    //删除
    @DeleteMapping("/project/{pid}")
    public int delete(@PathVariable("pid") int pid){
        int result = this.mapper.delete(pid);
        return result;
    }
}
